#!/bin/bash

shopt -s nullglob

. $LKP_SRC/lib/install.sh
. $LKP_SRC/distro/common
. $LKP_SRC/distro/package-manager/dnf
. $LKP_SRC/lib/debug.sh

distro_install_depends()
{
	local script
	local bm="$1"
	local scripts=$(find $LKP_SRC/distro/depends/ -name "$bm" -o -name "${bm}.[0-9]")

	support_repo_epel

	for script in $scripts
	do
		script=$(basename $script)
		packages=$(get_dependency_packages $DISTRO $script)

		[ -z "$packages" ] && continue

		echo install packages for $script: $packages

		local ocfs2_tools_name=$(echo "$packages" | grep 'ocfs2-tools')
		[ -n "$ocfs2_tools_name" ] && install_ocfs2_tools "$ocfs2_tools_name"

		ospkg_install $packages
	done
}

pack_benchmark()
{
	setup_proxy

	distro_install_depends lkp-dev

	# Process each benchmark
	for BM_NAME in $benchmark
	do
		distro_install_depends $BM_NAME-dev || continue
		echo $LKP_SRC/sbin/pack -d $DISTRO -f -c -s $PKG_MNT/$pack_to $BM_NAME
		(
			$LKP_SRC/sbin/pack -d $DISTRO -f -c -s $PKG_MNT/$pack_to $BM_NAME
		)
	done
}

yum_download()
{
	support_repo_epel

	echo "yum install -y --downloadonly --skip-broken $@ | grep -e \"No\ package .*\ available\""
	yum install -y --downloadonly --skip-broken "$@" | grep -e "No\ package .*\ available" && exit 1
	return 0
}

yum_pack()
{
	local target_dir=opt/rpms
	local date=$(date +"%Y%m%d")

	local downloaded_rpms=$(find /var/ -type f -name "*.rpm")

	[ "$downloaded_rpms" ] || return

	mkdir -p $target_dir

	mv $downloaded_rpms $target_dir

	[ ${suite} == "cci-depends" ] && distro_install_depends cci-depends
	find $target_dir | cpio --quiet -o -H newc --owner=root.root | gzip -n -9 >$pack_to/${BM_NAME}_$date.cgz

	ln -sf ${BM_NAME}_$date.cgz $pack_to/${BM_NAME}.cgz || return
	chown .lkp $pack_to/${BM_NAME}_$date.cgz $pack_to/${BM_NAME}.cgz || return
	echo package uploaded to $pack_to/${benchmark}.cgz

	ls $target_dir/*.rpm > $pack_to/.${BM_NAME}.packages

	rm -rf $target_dir
}

cleanup_downloaded_rpms()
{
	find /var/ -type f -name "*.rpm" -exec rm -f -- '{}' \;
}

cleanup_installed_depends_rpms()
{
	for package in $@
	do
		yum remove -y "$package" >/dev/null 2>&1
	done
}

pack_benchmark_deps()
{
	setup_proxy

	for BM_NAME in $benchmark
	do

		cleanup_downloaded_rpms

		check_shared_package $BM_NAME

		packages=$(get_dependency_packages $DISTRO $BM_NAME)

		cleanup_installed_depends_rpms $packages
		local ocfs2_tools_name=$(echo "$packages" | grep 'ocfs2-tools')
		[ -n "$ocfs2_tools_name" ] && download_ocfs2_tools "$ocfs2_tools_name"

		if yum_download $packages; then
			yum_pack
		else
			echo "failed to pack-deps $BM_NAME" >&2
			exit 1
		fi
	done
}

install_gcc7()
{
	if [ $DISTRO = "aliyun" ]; then
		wget -O /tmp/centos-release-scl-rh-2-3.el7.centos.noarch.rpm https://rpmfind.net/linux/centos/7/extras/x86_64/Packages/centos-release-scl-rh-2-3.el7.centos.noarch.rpm
		rpm -ivh --ignoresize /tmp/centos-release-scl-rh-2-3.el7.centos.noarch.rpm
	else
		yum install -y -q centos-release-scl-rh
	fi
	yum install -y -q "devtoolset-7-gcc*"
	source /opt/rh/devtoolset-7/enable
}

download_ocfs2_tools()
{
	local pkgs=$1

	mkdir -p /opt/rpms
	yum install -y -q wget

	wget -nc -P /opt/rpms https://yum.oracle.com/repo/OracleLinux/OL7/latest/x86_64/getPackage/ocfs2-tools-1.8.6-11.el7.x86_64.rpm

	local download_pkgs="/opt/rpms/ocfs2-tools-1.8.6-11.el7.x86_64.rpm"

	if echo "$pkgs" | grep -q 'ocfs2-tools-devel'; then
		wget -nc -P /opt/rpms https://yum.oracle.com/repo/OracleLinux/OL7/latest/x86_64/getPackage/ocfs2-tools-devel-1.8.6-11.el7.x86_64.rpm
		download_pkgs="$download_pkgs /opt/rpms/ocfs2-tools-devel-1.8.6-11.el7.x86_64.rpm"
	fi

	# Try to download the dependences of ocfs2-tools/ocfs2-tools-devel
	yum install -y -q --downloadonly "$download_pkgs"
}

install_ocfs2_tools()
{
	local pkgs=$1

	download_ocfs2_tools "$pkgs"

	yum install -y -q /opt/rpms/ocfs2-tools-1.8.6-11.el7.x86_64.rpm

	echo "$pkgs" | grep -q 'ocfs2-tools-devel' && \
		yum install -y -q /opt/rpms/ocfs2-tools-devel-1.8.6-11.el7.x86_64.rpm
}
